package cn.yh.ssm.service;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yh.ssm.dao.IStudentDao;
import cn.yh.ssm.model.Page;
import cn.yh.ssm.model.Student;

@Service
public class StudentService {

	// 日志
	private static Logger log = LogManager.getLogger(StudentService.class);

	@Autowired
	private IStudentDao studentDao;

	/**
	 * 根据id查找student
	 * 
	 * @param id
	 * @return
	 */
	public Student getById(Integer id) {

		return studentDao.findById(id);
	}

	/**
	 * 添加学生
	 * 
	 * @param student
	 */
	public void addStudent(Student student) {
		log.error("添加学生:" + student);
		studentDao.add(student);
	}

	/**
	 * 修改student
	 * 
	 * @param student
	 */
	public void updateStudent(Student student) {
		log.error("修改学生:" + student);
		studentDao.update(student);
	}

	/**
	 * 删除student
	 * 
	 * @param student
	 */
	public void deleteStudent(Student student) {
		log.error("删除学生:" + student);
		studentDao.deleteById(student.getId());
		;
	}

	/**
	 * 分页模糊查询
	 * 
	 * @param map
	 * @return
	 */
	public Page getLikeStudentByName(Map<String, Object> map) {
		Page page = new Page();
		int curPageNum = Integer.parseInt(String.valueOf(map.get("curPageNum")));
		page.setCurPageNum(curPageNum);
		int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
		page.setPageSize(pageSize);
		double count = studentDao.getLikeStudentByNameCount(map);
		Double totalPage = Math.ceil(count / pageSize); // 向上取整
		page.setTotalPage(totalPage.intValue());
		page.setList(studentDao.findLikeStudentByName(map));
		return page;

	}

}
